﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Project_V1
{
    public partial class FrmParamSet : Form
    {
        public FrmParamSet()
        {
            InitializeComponent();
            this.Load += FrmParamSet_Load;
            this.timer1.Interval = 200;
        }

        private void FrmParamSet_Load(object sender, EventArgs e)
        {
            this.timer1.Enabled = true;
        }

        //双击事件,将值传给参数修改界面,弹出界面
        private void ModifyTxt_UserControlClick(object sender, EventArgs e)
        {
            if (sender is TextBoxControl)
            {
                TextBoxControl item = (TextBoxControl)sender;

                string InitialValue = item.ActualValue;//将控件读取的ActualValue赋值给参数修改界面的Label显示

                string Variable = string.Empty;
                if (item.Tag != null)//由控件Tag的值得到变量名
                {
                    Variable = "." + item.Tag.ToString();//拼接变量名
                }
                FrmParaModify objFrm = new FrmParaModify(InitialValue, Variable);
                objFrm.ShowDialog();
            }
        }

        private void btn_PressConfirmed_Click(object sender, EventArgs e)
        {
            Beckhoff_PLC.IsWriting = true;
            if (CommonMethods.CurrentValue["PressConfirmed"] == "False")
            {
                Beckhoff_PLC.WriteVariable(".PressConfirmed", true);
            }
            if (CommonMethods.CurrentValue["PressConfirmed"] == "True")
            {
                Beckhoff_PLC.WriteVariable(".PressConfirmed", false);
            }
            Beckhoff_PLC.IsWriting = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            foreach (Control ct in this.Controls) // this.panel1.Controls)//遍历panel1内控件,并赋值
            {
                if (ct is Button && ct.Tag != null && ct.Tag.ToString() != "")
                {
                    if (CommonMethods.CurrentValue[ct.Tag.ToString()] == "True" && CommonMethods.CurrentValue.ContainsKey(ct.Tag.ToString()))
                    {
                        //ct.BackColor = Color.Green;
                        ct.BackColor = Color.FromArgb(60, 179, 113);
                    }
                    else
                    {
                        //ct.BackColor = Color.Gray;
                        ct.BackColor = Color.FromArgb(255, 255, 255);
                    }
                }
            }
        }

        private void FrmParamSet_Load_1(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Beckhoff_PLC.IsWriting = true;
            if (CommonMethods.CurrentValue["StopTorqueConfirmed"] == "False")
            {
                Beckhoff_PLC.WriteVariable(".StopTorqueConfirmed", true);
            }
            if (CommonMethods.CurrentValue["StopTorqueConfirmed"] == "True")
            {
                Beckhoff_PLC.WriteVariable(".StopTorqueConfirmed", false);
            }
            Beckhoff_PLC.IsWriting = false;
        }
    }
}
